home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2426 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Returning a variable from system() function
  5. Date: Sun, 21 Jan 1996 06:18:07 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4dslpn$4pf@s02.pavilion.co.uk>
  8. References: <295336694wnr@iiga.demon.co.uk>
  9. NNTP-Posting-Host: poolc46.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Pete Ryan <pete@iiga.demon.co.uk> wrote:
  13.  
  14. >Hiya,
  15. >    I`ve just been experimenting with C on UNIX and have come across a 
  16. >problem!.  There is a UNIX command called `find / -name gwire -print` 
  17. >which scans the UNIX drive for files/directories containing `gwire`.  
  18. >Anyway what I want to do is the following...
  19.  
  20. >#include <stdio.h>
  21. >#include <stdlib.h>
  22. >etc
  23.  
  24. >void main()
  25. >{
  26. >    char    tmp[];
  27. >    
  28. >    chdir("/usr2/willesden");    
  29. >    
  30. >    */ offending code below ;( */
  31. >    tmp = system("find . -name gwire");
  32.  
  33. >    printf("%s",tmp");
  34.  
  35. >}
  36.  
  37. >Therefore if I run `find . -name gwire -print` it returns all the 
  38. >directories containing `gwire`.  However the code above does not 
  39. >work!!. Is it because `= system` returns an integer and not strings???
  40.  
  41. >Any help would be very much appreciated as I have only just started 
  42. >learning c in the last 2 months.  
  43.  
  44. You have several alternatives:
  45.  
  46. 1) redirect output from find to a temporary file then read that file.
  47.    This only uses standard functions.  The command line is system
  48.    specific though.
  49.  
  50.     #include <stdio.h>
  51.     #include <stdlib.h>
  52.     
  53.     . . .
  54.     
  55.     char line[1024];
  56.     char name[1024];
  57.     FILE *input;
  58.     strcpy(line, "find . -name gwire > ");    /* fixed part of command */
  59.     strcpy(name, tmpnam(NULL));    /* get a suitable file name */
  60.     strcat(line, name);        /* add it to the end of the command */
  61.     if (system(line)==0)        /* do it */
  62.     { input = fopen(name, "r");    /* open the result */
  63.       /* read the file */
  64.       fclose(input);        /* close the result */
  65.         }
  66.     else
  67.     { /* handle error */
  68.     }
  69.  
  70.     remove(name);            /* clear up */
  71.  
  72. 2) Use Unix:
  73.     Create a pipe
  74.     fork() a child process.
  75.     In the child:
  76.     - redirect stdout to the pipe
  77.     - exec() the command
  78.     In the parent:
  79.     - read from the pipe until the child terminates (EOF?)
  80.  
  81. The advantage of the second method is that your program does not have
  82. to wait for "find" to finish before it can use the results - your
  83. program is more responsive.  If this interests you, read the FAQs in
  84. comp.unix.programmer.
  85.  
  86. Regards,
  87.         
  88. -----BEGIN PGP PUBLIC KEY BLOCK-----
  89. Version: 2.6.2i
  90.  
  91. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  92. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  93. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  94. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  95. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  96. =/wVD
  97. -----END PGP PUBLIC KEY BLOCK-----
  98.  
  99.